/*
 * @Author: shawnxiao
 * @Date: 2021-04-11 11:42:48
 * @LastEditTime: 2021-10-15 10:31:48
 * @FilePath: /project/src/index/store/user/reducer.tsx
 */
import { Reducer } from 'redux'
import { IAction } from '../types'
import {
  //  getStorage,
   setStorage, removeStorage } from '@/common/utils/storage'
import { 
  // getCookie,
   setCookie, removeCookie } from '@/common/utils/cookie'
import {getTokenKeyForPage} from '@/common/utils'

import * as constants from './constants'

export interface UserState {
  token: string | null;
  userInfo:any
  realName?: string;
  corpName?: string;
}

// 依据页面html生成的token_key
const TokenKeyForPage = getTokenKeyForPage()

const defaultState: UserState = {
  userInfo: {},
  token: ''
  // userInfo: {...getStorage(constants.USER_INFO_KEY)},
  // token: getCookie(TokenKeyForPage)
}

const reducer: Reducer<UserState, IAction<any>> = (state = defaultState, action: IAction<any>) => {
  const { type, payload } = action
  switch (type) {
    case constants.SET_USER_INFO:
      setCookie(TokenKeyForPage, payload?.token)
      setStorage(constants.USER_INFO_KEY, payload)
      return {
        ...payload
      }
    case constants.SET_USER_LOGOUT:
      removeCookie(TokenKeyForPage)
      removeStorage(constants.USER_INFO_KEY)
      return {
        ...defaultState
      }
    default:
      return state
  }
}

export default reducer
